package service.menu.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import dao.menu.MenuDao;
import entities.menu.MenuEntities;
import entities.permission.Permission;
import service.menu.MenuService;
import util.base.CurrentUser;
@Service
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    /**
     * 获得所有菜单
     */
	@Override
	public MenuEntities getAllMenu() {
		List<MenuEntities> menus=menuDao.getMenu();
		MenuEntities menu=new MenuEntities();
		for(MenuEntities mn:menus){
			if(mn.getLevel()==0){
				menu=mn;
				break;
			}
		}
		//获得所有等级
		Set<Integer> levels=new HashSet<Integer>();
		for(MenuEntities mn:menus){
			levels.add(mn.getLevel());
		}
		//set转list
		List<Integer> levelList=new ArrayList<Integer>(levels);
		//排序
		Collections.sort(levelList);
		List<MenuEntities> currentMenu=new ArrayList<MenuEntities>();
		//获得最高等级的数据
		for(MenuEntities mn:menus){
			if(mn.getLevel()==levelList.get(levelList.size()-1)){
				currentMenu.add(mn);
			}
		}
		for(int i=levelList.size()-1;i>1;i--){
		List<MenuEntities> lastMenu=new ArrayList<MenuEntities>();
		//获得当前等级和上个等级的数据
		for(MenuEntities mn:menus){
			if(mn.getLevel()==(levelList.get(i)-1)){
				lastMenu.add(mn);
			}
		}
		//把当前等级的数据写入上一个等级
		for(int j=0;j<lastMenu.size();j++){
			List<MenuEntities> matchingMenu=new ArrayList<MenuEntities>();
			for(int z=0;z<currentMenu.size();z++){
				if(currentMenu.get(z).getParentId().equals(lastMenu.get(j).getId())){
					matchingMenu.add(currentMenu.get(z));
				}
			}
			lastMenu.get(j).setChildren(matchingMenu);	
		}
		currentMenu=lastMenu;
		}
		menu.setChildren(currentMenu);
		return menu;
	}
    /**
     * 获得已授权菜单
     */
	@Override
	public MenuEntities getAuthorizedMenu() {
		List<MenuEntities> menus=menuDao.getMenu();
		//判断是否有权限
		Subject currentUser = SecurityUtils.getSubject();
		for(int i=0;i<menus.size();){
			if(!currentUser.isPermitted(menus.get(i).getId())&&menus.get(i).getLevel()!=0){
				menus.remove(i);
			}else{
				i++;
			}
		}
		MenuEntities menu=new MenuEntities();
		for(MenuEntities mn:menus){
			if(mn.getLevel()==0){
				menu=mn;
				break;
			}
		}
		//获得所有等级
		Set<Integer> levels=new HashSet<Integer>();
		for(MenuEntities mn:menus){
			levels.add(mn.getLevel());
		}
		//set转list
		List<Integer> levelList=new ArrayList<Integer>(levels);
		//排序
		Collections.sort(levelList);
		List<MenuEntities> currentMenu=new ArrayList<MenuEntities>();
		//获得最高等级的数据
		for(MenuEntities mn:menus){
			if(mn.getLevel()==levelList.get(levelList.size()-1)){
				currentMenu.add(mn);
			}
		}
		for(int i=levelList.size()-1;i>1;i--){
		List<MenuEntities> lastMenu=new ArrayList<MenuEntities>();
		//获得当前等级和上个等级的数据
		for(MenuEntities mn:menus){
			if(mn.getLevel()==(levelList.get(i)-1)){
				lastMenu.add(mn);
			}
		}
		//把当前等级的数据写入上一个等级
		for(int j=0;j<lastMenu.size();j++){
			List<MenuEntities> matchingMenu=new ArrayList<MenuEntities>();
			for(int z=0;z<currentMenu.size();z++){
				if(currentMenu.get(z).getParentId().equals(lastMenu.get(j).getId())){
					matchingMenu.add(currentMenu.get(z));
				}
			}
			lastMenu.get(j).setChildren(matchingMenu);	
		}
		currentMenu=lastMenu;
		}
		menu.setChildren(currentMenu);
		return menu;
	}
	/**
	 * 新增或更新菜单
	 */
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void addMenu(int type,String id,int level,int location,String name,String path,HttpServletRequest req) {
		MenuEntities menu=new MenuEntities();
		String currentUser=CurrentUser.getCurrentUser().getName();
		menu.setText(name);
		menu.setPath(path);
		//新增子菜单（0）或一级菜单（3）
		if(type==0||type==3){
			int maxLocation=0;
			if(null!=menuDao.getSubmenuMaxLocation(id)){
				maxLocation=menuDao.getSubmenuMaxLocation(id);
			}
			String menuId=UUID.randomUUID().toString();
			menu.setId(menuId);
			menu.setLocation(maxLocation+1);
			if(type==0){
				menu.setParentId(id);
				menu.setLevel(level+1);
			 }else{
				menu.setLevel(1);
				menu.setParentId("1");
			}
			menu.setAdd_person(currentUser);
			menu.setAdd_time(new Date());
			
			Permission permission=new Permission();
			permission.setId(UUID.randomUUID().toString());
			permission.setName(menuId);
			permission.setRemark(name);
			permission.setAdd_person(currentUser);
			permission.setAdd_time(new Date());
			menuDao.insertMenu(menu);
		//修改菜单
		}else if(type==1){
			menu.setId(id);
			menu.setUpdate_person(currentUser);
			menu.setUpdate_time(new Date());
			menuDao.updateMenu(menu);
		}
	}
	@Override
	public void deleteMenu(String id) {
		menuDao.deleteMenu(id);
	}

}
